package org.itzixi.filter;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.itzixi.RedisKeyConstant;
import org.itzixi.RequestConstant;
import org.itzixi.grace.result.ResponseStatusEnum;
import org.itzixi.utils.ExcludeUrlProperties;
import org.itzixi.utils.RedisOperator;
import org.itzixi.utils.RenderErrorMsgUtil;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@Slf4j
@RefreshScope
public class SecurityFilter implements GlobalFilter, Ordered {

    @Resource
    private ExcludeUrlProperties excludeUrlProperties;

    @Resource
    private RedisOperator redisOperator;

//    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String path = exchange.getRequest().getURI().getPath();
        log.info("当前请求的路径为：{}", path);
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        List<String> urlList = excludeUrlProperties.getUrls();
        if (!CollectionUtil.isEmpty(urlList)){
            for (String excludeUrl : urlList) {
                if (antPathMatcher.matchStart(excludeUrl, path)){
                    return chain.filter(exchange);
                }
            }

        }

        String fileStart = excludeUrlProperties.getFileStart();
        if (StrUtil.isNotBlank(fileStart)){
            boolean matchFileStart = antPathMatcher.matchStart(fileStart, path);
            if (matchFileStart) return chain.filter(exchange);
        }

        // 判断header中是否有token
        HttpHeaders headers = exchange.getRequest().getHeaders();
        String headUserId = headers.getFirst(RequestConstant.HEADER_USER_ID);
        String headUserToken = headers.getFirst(RequestConstant.HEADER_USER_TOKEN);
        log.info("headUserId：{}", headUserId);
        log.info("headUserToken：{}", headUserToken);
        if (StrUtil.isNotBlank(headUserId) && StrUtil.isNotBlank(headUserToken)){
//            String redisUserToken = redisOperator.get(RedisKeyConstant.REDIS_USER_TOKEN + headUserId);
//            log.info("redisUserToken：{}", redisUserToken);
//            if (headUserToken.equalsIgnoreCase(redisUserToken)){
//                return chain.filter(exchange);
//            }
            String redisUserId = redisOperator.get(RedisKeyConstant.REDIS_USER_TOKEN + headUserToken);
            log.info("redisUserId：{}", redisUserId);
            if (headUserId != null && headUserId.equalsIgnoreCase(redisUserId)){
                return chain.filter(exchange);
            }
        }
        log.info("当前请求的路径不被放行,您还未登录：{}", path);
        return RenderErrorMsgUtil.renderErrorMsg(exchange, ResponseStatusEnum.UN_LOGIN);
    }





    @Override
    public int getOrder() {
        return 0;
    }
}
